package com.sitanyun.merchant.guide.frament.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.QueryMoneyBean;
import com.sitanyun.merchant.guide.adapter.UserMoneyAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.QueryuserMoneyBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 我的钱包
 */
public class UserMoneyActivity extends BaseActivity {


    @BindView(R.id.moneyall)
    TextView moneyall;
    @BindView(R.id.moneyvr)
    TextView moneyvr;
    @BindView(R.id.moneylj)
    TextView moneylj;
    @BindView(R.id.moneytx)
    TextView moneytx;
    @BindView(R.id.succes)
    TextView succes;
    @BindView(R.id.ll_nomoney)
    LinearLayout llNomoney;
    @BindView(R.id.rv_usermoney)
    RecyclerView rvUsermoney;
    @BindView(R.id.stateImg)
    ImageView stateImg;
    @BindView(R.id.img_point)
    ImageView imgPoint;
    private QueryMoneyBean queryMoneyBean;
    private UserMoneyAdapter userMoneyAdapter;
    private PopupWindow popupWindow;
    private Toast toast;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_money;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("我的奖励");

        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(UserMoneyActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvUsermoney.setLayoutManager(layoutManager);
        userMoneyAdapter = new UserMoneyAdapter(UserMoneyActivity.this);
        rvUsermoney.setAdapter(userMoneyAdapter);
        userMoneyAdapter.notifyDataSetChanged();

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtils.get().url(Urls.balance)
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                queryMoneyBean = new Gson().fromJson(response, QueryMoneyBean.class);
                if (queryMoneyBean.getCode() == 0) {
                    moneyall.setText("¥ " + queryMoneyBean.getData().getTotalBalance());
                    moneyvr.setText("¥ " + queryMoneyBean.getData().getVrBalance());
                    moneylj.setText("¥ " + queryMoneyBean.getData().getExpenditure());
                    if (queryMoneyBean.getData().getVrBalance() == 0) {
                        moneytx.setBackground(getDrawable(R.drawable.money_radios));
                        moneyvr.setText("暂无收益");
                    } else {
                        moneytx.setBackground(getDrawable(R.drawable.money_radio));
                    }

                }
            }
        });
        initokhttps("99");
    }

    // 查询
    private void initokhttps(String choose) {
        //     99全部  1VR    4提现
        OkHttpUtils.get().url(Urls.bill)
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .addParams("page", "0")
                .addParams("size", "5")
                .addParams("choose", choose)
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                QueryuserMoneyBean queryuserMoneyBean = new Gson().fromJson(response, QueryuserMoneyBean.class);
                if (queryuserMoneyBean.getCode() == 0) {
                    if (queryuserMoneyBean.getData().getRecords().size() == 0) {
                        llNomoney.setVisibility(View.VISIBLE);
                        rvUsermoney.setVisibility(View.GONE);
                    } else {
                        llNomoney.setVisibility(View.GONE);
                        rvUsermoney.setVisibility(View.VISIBLE);
                        userMoneyAdapter.setNewData(queryuserMoneyBean.getData().getRecords());

                    }
                }

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.moneytx, R.id.succes})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.moneytx:
                if (SharedPreferenceUtil.getStringData("isindividuals").equals("0")) {
                    dialogs();
                    return;
                } else if (queryMoneyBean.getData().getVrBalance() == 0) {
                    ToastUtil.showToast(UserMoneyActivity.this, "暂无提现金额");
                    return;
                }
//                else
//                    if (queryMoneyBean.getData().getVrBalance() == 0) {
//                        ToastUtil
//                    return;
//                }
                else {
                    startActivity(new Intent(UserMoneyActivity.this, AmountwithdrawalActivity.class)
                            .putExtra("allmoney", moneyall.getText().toString().trim())
                            .putExtra("moneyvr", moneyvr.getText().toString().trim()));

                }
                break;
            case R.id.succes:
                stateImg.animate().setDuration(500).rotation(180).start();
                initpopu(view);
                break;
        }
    }

    //推广筛选
    private void initpopu(View view) {
        View contentView = LayoutInflater.from(UserMoneyActivity.this).inflate(
                R.layout.money_item, null);
        Button alls = (Button) contentView.findViewById(R.id.alls);
        Button online = (Button) contentView.findViewById(R.id.online);
        Button ofline = (Button) contentView.findViewById(R.id.ofline);

        //全部
        alls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                succes.setText("全部");
                initokhttps("99");
                //     99全部  1VR    4提现
                userMoneyAdapter.notifyDataSetChanged();
                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();

            }
        });
        //VR
        online.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                succes.setText("VR云逛店推广激励");
                initokhttps("1");
                userMoneyAdapter.notifyDataSetChanged();
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();

            }
        });
        //提现
        ofline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                succes.setText("提现");
                initokhttps("4");
                userMoneyAdapter.notifyDataSetChanged();
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();

            }
        });

        popupWindow = new PopupWindow(contentView,
                400, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                stateImg.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));
//       popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//       int xPos = view.getWidth() / 2 - popupWindow.getContentView().getMeasuredWidth() / 2;
//       // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
        //   popupWindow.showAtLocation(todayState, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }

//    /**
//     * 弹窗银行卡
//     */
//    private void dialog() {
//        //1、使用Dialog、设置style
//        final Dialog dialog = new Dialog(UserMoneyActivity.this, R.style.DialogTheme);
//        //2、设置布局
//        View view = View.inflate(UserMoneyActivity.this, R.layout.item_banks, null);
//        dialog.setContentView(view);
//        view.findViewById(R.id.qx);
//        view.findViewById(R.id.qx).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        view.findViewById(R.id.qrz).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(UserMoneyActivity.this, BankActivity.class).putExtra("isIndividual", getIntent().getStringExtra("isIndividual")));
//                dialog.dismiss();
//            }
//        });
//    }

    /**
     * 弹窗
     */
    private void dialogs() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(UserMoneyActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(UserMoneyActivity.this, R.layout.item_banks, null);
        dialog.setContentView(view);
        view.findViewById(R.id.qx);
        view.findViewById(R.id.qx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.qrz).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(UserMoneyActivity.this, RealNameActivity.class)
                        .putExtra("name", getIntent().getStringExtra("name"))
                        .putExtra("isIndividual", getIntent().getStringExtra("isIndividual")));
                dialog.dismiss();
            }
        });
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);
    }
    //提示窗
    @OnClick(R.id.img_point)
    public void onViewClicked() {
       // toast.show();
        dialog();
    }

    private void dialog() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(UserMoneyActivity.this, R.style.DialogThemes);
        //2、设置布局
        View view = View.inflate(UserMoneyActivity.this, R.layout.money_tocat, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);
    }
}
